package org.visionlibrary.image.filters.effects;

import java.awt.Point;

import org.visionlibrary.image.model.ConvolutionKernel;


public class Emboss {
	protected int size = 3;

	protected int direction = 1;

	protected Point origin = new Point(1, 1);

	public int getDirection() {
		return direction;
	}

	public Point getOrigin() {
		return origin;
	}

	public int getSize() {
		return size;
	}

	public void setOrigin(Point origin) {
		this.origin = origin;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

	public ConvolutionKernel getFilterKernel() {
		if (direction == NORTH)
			return this.getNorthKernel();
		else if (direction == SOUTH)
			return this.getSouthKernel();
		else if (direction == WEST)
			return this.getWestKernel();
		else if (direction == EAST)
			return this.getEastKernel();
		else if (direction == NORTH_WEST)
			return this.getNorthWestKernel();
		else if (direction == SOUTH_WEST)
			return this.getSouthWestKernel();
		else if (direction == NORTH_EAST)
			return this.getNorthEastKernel();
		else
			return this.getSouthEastKernel();
	}

	protected ConvolutionKernel getNorthEastKernel() {
		if (size == 0)
			return null;
		float[] values = new float[size * size];

		int i = 0;
		while (i < size - 1) {
			int j = 1 + i;
			while (j < size) {
				values[i * size + j] = 1.0f;
				j++;
			}
			i++;
		}

		i = 1;
		while (i < size) {
			int j = 0;
			while (j < i) {
				values[i * size + j] = -1.0f;
				j++;
			}
			i++;
		}

		return new ConvolutionKernel(size, size, values, 1.0f, new Point(size / 2,
				size / 2));
	}

	protected ConvolutionKernel getSouthWestKernel() {
		if (size == 0)
			return null;
		float[] values = new float[size * size];

		int i = 0;
		while (i < size - 1) {
			int j = 1 + i;
			while (j < size) {
				values[i * size + j] = -1.0f;
				j++;
			}
			i++;
		}

		i = 1;
		while (i < size) {
			int j = 0;
			while (j < i) {
				values[i * size + j] = 1.0f;
				j++;
			}
			i++;
		}

		return new ConvolutionKernel(size, size, values, 1.0f, new Point(size / 2,
				size / 2));
	}

	protected ConvolutionKernel getNorthWestKernel() {
		if (size == 0)
			return null;
		float[] values = new float[size * size];

		int i = 0;
		while (i < size - 1) {
			int j = 0;
			while (j < size - 1 - i) {
				values[i * size + j] = 1.0f;
				j++;
			}
			i++;
		}

		i = 1;
		while (i < size) {
			int j = size - 1;
			while (j > size - i - 1) {
				values[i * size + j] = -1.0f;
				j--;
			}
			i++;
		}

		return new ConvolutionKernel(size, size, values, 1.0f, new Point(size / 2,
				size / 2));
	}

	protected ConvolutionKernel getSouthEastKernel() {
		if (size == 0)
			return null;
		float[] values = new float[size * size];

		int i = 0;
		while (i < size - 1) {
			int j = 0;
			while (j < size - 1 - i) {
				values[i * size + j] = -1.0f;
				j++;
			}
			i++;
		}

		i = 1;
		while (i < size) {
			int j = size - 1;
			while (j > size - i - 1) {
				values[i * size + j] = 1.0f;
				j--;
			}
			i++;
		}

		return new ConvolutionKernel(size, size, values, 1.0f, new Point(size / 2,
				size / 2));
	}

	protected ConvolutionKernel getEastKernel() {
		if (size == 0)
			return null;
		float[] values = new float[size * size];

		int i = 0;
		while (i < size) {
			int j = 0;
			while (j < size) {
				if (j < size / 2)
					values[i * size + j] = 1.0f;
				else if (j > size / 2)
					values[i * size + j] = -1.0f;
				j++;
			}
			i++;
		}

		return new ConvolutionKernel(size, size, values, 1.0f, new Point(size / 2,
				size / 2));
	}

	protected ConvolutionKernel getWestKernel() {
		if (size == 0)
			return null;
		float[] values = new float[size * size];

		int i = 0;
		while (i < size) {
			int j = 0;
			while (j < size) {
				if (j < size / 2)
					values[i * size + j] = -1.0f;
				else if (j > size / 2)
					values[i * size + j] = 1.0f;
				j++;
			}
			i++;
		}

		return new ConvolutionKernel(size, size, values, 1.0f, new Point(size / 2,
				size / 2));
	}

	protected ConvolutionKernel getSouthKernel() {
		if (size == 0)
			return null;
		float[] values = new float[size * size];

		int start = (size / 2) * size;
		int stop = start + size;

		java.util.Arrays.fill(values, 0, start, -1.0f);
		java.util.Arrays.fill(values, stop, values.length, 1.0f);

		System.out.println(java.util.Arrays.toString(values));
		return new ConvolutionKernel(size, size, values, 1.0f, new Point(size / 2,
				size / 2));
	}

	protected ConvolutionKernel getNorthKernel() {
		if (size == 0)
			return null;
		float[] values = new float[size * size];

		int start = (size / 2) * size;
		int stop = start + size;

		java.util.Arrays.fill(values, 0, start, 1.0f);
		java.util.Arrays.fill(values, stop, values.length, -1.0f);

		System.out.println(java.util.Arrays.toString(values));
		return new ConvolutionKernel(size, size, values, 1.0f, new Point(size / 2,
				size / 2));
	}

	public static final int NORTH = 1;

	public static final int SOUTH = 2;

	public static final int EAST = 3;

	public static final int WEST = 4;

	public static final int NORTH_EAST = 5;

	public static final int NORTH_WEST = 6;

	public static final int SOUTH_EAST = 7;

	public static final int SOUTH_WEST = 8;
}
